package vista;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JInternalFrame;

import excepciones.ExcepcionConvertirDouble;
import excepciones.ExcepcionConvertirFloat;
import excepciones.ExcepcionConvertirInteger;


public abstract class Ventana extends JInternalFrame
{
	private static final long serialVersionUID = 1L;
	private static final int SEPARACION = 30;
	static int inset = 0;
	
	private List<Ventana> observers = new ArrayList<Ventana>();
	
	protected Principal parent;

	public Ventana(Principal Parent)
	{
		super(null,true,true,true,true);
        parent = Parent;
        setLocation(SEPARACION*inset, SEPARACION*inset);
        
	}
	
	public Integer convertToInt(String s) throws ExcepcionConvertirInteger{
		try{
			return Integer.parseInt(s);
		}catch(Exception e)
		{
			throw new ExcepcionConvertirInteger();
		}
	}
	
	public Float convertToFloat(String s) throws ExcepcionConvertirFloat{
		try{
			return Float.parseFloat(s);
		}catch(Exception e)
		{
			throw new ExcepcionConvertirFloat();
		}
	}
	
	public Double convertToDouble(String s) throws ExcepcionConvertirDouble{
		try{
			return Double.parseDouble(s);
		}catch(Exception e)
		{
			throw new ExcepcionConvertirDouble();
		}
	}
	
	public void refrescar()
	{
	}
	
	public void cerrar()
	{
    	try
    	{
    		this.setClosed(true);
    		this.dispose();
        }
    	catch (Exception e)
    	{
        }
        if (parent.getCantidadInternalFrames() == 0) {
    		parent.configurarOpciones(false);
        }
    }
	
	public void addObserver(Ventana frm){
		observers.add(frm);
	}
	
	public void removeObserver(Ventana frm){
		observers.remove(frm);
	}
	
	public void updateObservers(){
		Iterator<Ventana> i = observers.iterator();
		while(i.hasNext())
		{
			Ventana item = i.next();
			item.refrescar();
		}
	}
}
